package com.myteam.mystory.DAO;

import java.util.ArrayList;
import java.util.List;

import com.myteam.mystory.entity.ParamEntity;
import com.myteam.mystory.entity.StoriesEntity;
import com.myteam.mystory.utils.LogUtils;

import android.R.integer;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StoriesDAO {
	public static final String TAG = "StoriesDAO";
	private final SQLiteDatabase db;
	
	public StoriesDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_story(StoriesEntity storiesEntity){
		ContentValues value = new ContentValues();
		value.put(StoriesScheme.STORYID, storiesEntity.getStoryId());
		value.put(StoriesScheme.CATEID, storiesEntity.getCateID());
		value.put(StoriesScheme.NAME, storiesEntity.getName());
		value.put(StoriesScheme.ACODE, storiesEntity.getaCode());
		value.put(StoriesScheme.IMGLINK, storiesEntity.getImgLink());
		value.put(StoriesScheme.DESCRIPTION, storiesEntity.getDescription());
		value.put(StoriesScheme.BOOKMARK, storiesEntity.getBookmark());
		value.put(StoriesScheme.HISTORY, storiesEntity.getHistory());
		value.put(StoriesScheme.VIEWCOUNT, storiesEntity.getViewCount());
		value.put(StoriesScheme.LIKECOUNT, storiesEntity.getLikeCount());
		value.put(StoriesScheme.STATUT, storiesEntity.getStatut());
		if (db.insert(StoriesScheme.TABLE_STORY_NAME, null, value) == -1) {
			LogUtils.e("Bug insert stories");
		} else {
			LogUtils.e("successfully insert stories");
		}
		return 0;
	}
	
	public ArrayList<StoriesEntity> getAllStory() {
		ArrayList<StoriesEntity> list_story = new ArrayList<StoriesEntity>();
		String query = "select * from " + StoriesScheme.TABLE_STORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()){
			do {
				StoriesEntity storiesEntity = new StoriesEntity();
				storiesEntity.setStoryId(cursor.getString(0));
				storiesEntity.setCateID(cursor.getString(1));//(cursor.getString(0));
				storiesEntity.setName(cursor.getString(2));
				storiesEntity.setaCode(cursor.getString(3));
				storiesEntity.setImgLink(cursor.getString(4));
				storiesEntity.setDescription(cursor.getString(5));
				storiesEntity.setBookmark(cursor.getInt(6));
				storiesEntity.setHistory(cursor.getInt(7));
				storiesEntity.setViewCount(cursor.getInt(8));
				storiesEntity.setLikeCount(cursor.getInt(9));
				storiesEntity.setStatut(cursor.getString(10));
				list_story.add(storiesEntity);
			} while (cursor.moveToNext());
		}
		return list_story;
	}
	
	public List<String> getNameStory(){
		List<String> listNameStory = new ArrayList<String>();
		String query = "select * from " + StoriesScheme.TABLE_STORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			do {
				String nameStory = cursor.getString(1);
				listNameStory.add(nameStory);
			} while (cursor.moveToNext());
		}
		return listNameStory;
	}
	
	public List<String> getStoryID(){
		List<String> listStoryId = new ArrayList<String>();
		String query = "select * from " + StoriesScheme.TABLE_STORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			do {
				String StoryId = cursor.getString(0);
				listStoryId.add(StoryId);
			} while (cursor.moveToNext());
		}
		return listStoryId;
	}
	
	
	
	public List<Integer> getViewCount(){
		List<Integer> listViewCount = new ArrayList<Integer>();
		String query = "select * from "+ StoriesScheme.TABLE_STORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()) {
			do {
				int viewCount = cursor.getInt(7);
				listViewCount.add(viewCount);
			} while (cursor.moveToNext());
		}
		return listViewCount;
	}
	
	public List<Integer> getlikeCount(){
		List<Integer> listlikeCount = new ArrayList<Integer>();
		String query = "select * from "+ StoriesScheme.TABLE_STORY_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()) {
			do {
				int likeCount = cursor.getInt(8);
				listlikeCount.add(likeCount);
			} while (cursor.moveToNext());
		}
		return listlikeCount;
	}
	
	public long deleteTableStory(){
		long result = 0;
		try {
			result = db.delete(StoriesScheme.TABLE_STORY_NAME, null, null);
			LogUtils.e("delete table category successfully");
		} catch (Exception e) {
			LogUtils.e("delete table category fail");
		}
		return result;
	}
}
